/**
 * 
 */
package ua.ws.feedsreader.actions;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import ua.ws.feedsreader.common.User;
import ua.ws.feedsreader.db.SemanticManager;
import ua.ws.feedsreader.utils.Log;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author ricardo
 *
 */
public class NewAccount extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7217097122746468222L;

	//inner params
	private String username;
	private String password;
	private String firstname;
	private String lastname;
	private String email;

	//out parameters
	private String errorRegisterMessage;

	@Override
	@Action(results={
			@Result(name="success", location="choose-categories", type="redirect" ),
			@Result(name="error", location="login.jsp" )
	})
	public String execute() throws Exception {

		Map<String, Object> session = ActionContext.getContext().getSession();
		
		SemanticManager semanticManager = new SemanticManager();
		boolean register = semanticManager.register(username, password, firstname, lastname, email);

		Log.debug("username: " + username );
		Log.debug("firstname: " + firstname );
		Log.debug("lastname: " + lastname );
		
		if( register == false ) {
			setErrorRegisterMessage("Já existe um utilizador com este login!");
			return ERROR;
		}

		session.put("USER", new User(username, firstname, lastname, email) );
		
		Log.debug("Register for " + username + " is correct!");

		return SUCCESS;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the firstname
	 */
	public String getFirstname() {
		return firstname;
	}

	/**
	 * @param firstname the firstname to set
	 */
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	/**
	 * @return the lastname
	 */
	public String getLastname() {
		return lastname;
	}

	/**
	 * @param lastname the lastname to set
	 */
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the errorRegisterMessage
	 */
	public String getErrorRegisterMessage() {
		return errorRegisterMessage;
	}

	/**
	 * @param errorRegisterMessage the errorRegisterMessage to set
	 */
	public void setErrorRegisterMessage(String errorRegisterMessage) {
		this.errorRegisterMessage = errorRegisterMessage;
	}

}
